package com.shop.loan.controller;

import com.shop.loan.annotation.LoginAuth;
import com.shop.loan.model.User;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.Constant;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.UserByMybatis;
import com.shop.loan.model.entity.UserContactByMybatis;
import com.shop.loan.model.vo.*;
import com.shop.loan.qiniu.QiNiuUtil;
import com.shop.loan.service.UserService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@Api(value = "用户管理", tags = "用户管理")
@RestController
public class UserController {

    @Resource
    UserService userService;

    @Resource
    QiNiuUtil qiNiuUtil;

    @LoginAuth
    @ApiOperation("用户详情")
    @ApiResponses({
            @ApiResponse(code = 1, message = "成功", response = User.class)
    })
    @PostMapping(Constant.appPrefex + "/user/detail")
    public ResResult detail(@ApiIgnore RentalUserVo rentalUser) {
        UserByMybatis userByMybatis = userService.detail(new UserLoginVo(rentalUser.getOpId()));
        if (userByMybatis != null) {
            return new ResResult(Code.SUCCESS, userByMybatis);
        }
        return new ResResult(Code.FAIL);
    }


//    @EruptRouter(
//            verifyType = EruptRouter.VerifyType.LOGIN
//    )
//    @PostMapping(EruptRestPath.ERUPT_API + "/user/detail")
//    public ResResult detailByAdmin(@ApiIgnore OrderVo orderVo) {
//        LoanOrderVo loanOrderVo = new LoanOrderVo();
//        loanOrderVo.setUserId(orderVo.getOpId());
//        return userService.detailByOrderId(loanOrderVo);
//    }

//    @EruptRouter(
//            verifyType = EruptRouter.VerifyType.LOGIN
//    )
//    @PostMapping(EruptRestPath.ERUPT_API + "/user/refreshBlack")
//    public ResResult refreshBlack(@ApiIgnore OrderVo orderVo) {
//        if (orderVo.getOrderId() == null) {
//            return new ResResult(Code.PARAM_LACK);
//        }
//        return userService.rdUserCheck(orderVo.getOrderId());
//    }


    @ApiOperation(value = "用户登录-获取图形验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码", dataType = "string", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 1, message = "成功")
    })
    @PostMapping(Constant.appPrefex + "/user/getPicCode")
    public ResResult getPicCode(@ApiIgnore UserLoginVo userLoginVo) {
        return userService.getPicCode(userLoginVo);
    }

    @ApiOperation(value = "用户登录-获取短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码-使用aes加密", dataType = "string", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 1, message = "成功")
    })
    @PostMapping(Constant.appPrefex + "/user/loginGetCode")
    public ResResult loginGetCode(@ApiIgnore UserLoginVo userLoginVo) {
        return userService.getMobileCode(userLoginVo, true);
    }


    @ApiOperation(value = "用户登录-提交短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码-使用aes加密", dataType = "string", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "string", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 1, message = "成功")
    })
    @PostMapping(Constant.appPrefex + "/user/loginByCode")
    public ResResult loginByCode(@ApiIgnore UserLoginVo userLoginVo) {
        return userService.loginByCode(userLoginVo);
    }


    @LoginAuth
    @ApiOperation(value = "用户身份信息编辑/新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "不传id则新增", dataType = "int"),
            @ApiImplicitParam(name = "idCardName", value = "姓名", dataType = "string", required = true),
            @ApiImplicitParam(name = "idCardNumber", value = "身份证号码", dataType = "string", required = true),
            @ApiImplicitParam(name = "idCardPicFront", value = "身份证正面----base64字符串", dataType = "string", required = true),
            @ApiImplicitParam(name = "idCardPicBack", value = "身份证反面----base64字符串", dataType = "string", required = true),
            @ApiImplicitParam(name = "ocrInfo", value = "ocr结果信息", dataType = "string"),
    })
    @PostMapping(Constant.appPrefex + "/user/identity/update")
    public ResResult addIdentity(@ApiIgnore CreateAuthVo createAuthVo) throws Exception {
        return userService.createIdentityUrl(createAuthVo);
    }


    @ApiOperation(value = "ocr识别身份证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "url", value = "身份证url（完整链接）", dataType = "string", required = true),
            @ApiImplicitParam(name = "isFront", value = "是否正面   1：正面  0：反面", dataType = "int", required = true),
    })
    @PostMapping(Constant.appPrefex + "/ocr/get")
    public ResResult ocr(@ApiIgnore OcrVo ocrVo) {
        return userService.getOcrInfo(ocrVo);
    }

    @ApiIgnore
    @PostMapping(Constant.appPrefex + "/user/addOtherInfo")
    public ResResult otherInfo(@ApiIgnore UserContactByMybatis userContactByMybatis) {
        return userService.contactAuth(userContactByMybatis);
    }



    @ApiOperation(value = "ocr识别银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "url", value = "银行卡url（完整链接）", dataType = "string", required = true),
    })
    @PostMapping(Constant.appPrefex + "/ocr/getOcrByBank")
    public ResResult getOcrByBank(@ApiIgnore OcrVo ocrVo) {
        return userService.getOcrByBank(ocrVo);
    }

    @LoginAuth
    @ApiOperation(value = "获取实名认证Url")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardName", value = "身份证姓名", dataType = "string", required = true),
            @ApiImplicitParam(name = "cardNum", value = "身份证号码", dataType = "string", required = true),
            @ApiImplicitParam(name = "idCardPicFront", value = "身份证正面（不包含文件前缀域名）", dataType = "string", required = true),
            @ApiImplicitParam(name = "idCardPicBack", value = "身份证反面（不包含文件前缀域名）", dataType = "string", required = true),
            @ApiImplicitParam(name = "ocrInfoByFront", value = "身份证正面Ocr结果", dataType = "string", required = true),
            @ApiImplicitParam(name = "ocrInfoByBack", value = "身份证正面Ocr结果", dataType = "string", required = true),
    })
    @PostMapping(Constant.appPrefex + "/user/createIdentityUrl")
    public ResResult createIdentityUrl(@ApiIgnore CreateAuthVo createAuthVo) {
        return userService.createIdentityUrl(createAuthVo);
    }

    @LoginAuth
    @ApiOperation(value = "活体认证成功")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serialNo", value = "活体流水号", dataType = "string", required = true),
    })
    @PostMapping(Constant.appPrefex + "/user/personAuthFaceSuccess")
    public ResResult personAuthFaceSuccess(@ApiIgnore CreateAuthVo createAuthVo) {
        return userService.personAuthFaceSuccess(createAuthVo);
    }




    @LoginAuth
    @ApiOperation(value = "银行卡认证获取验证码")
    @PostMapping(Constant.appPrefex + "/user/bankAuthGetCode")
    public ResResult bankAuthGetCode(@ApiIgnore CreateAuthVo createAuthVo) {
        return userService.bankAuthGetCode(createAuthVo);
    }

    @LoginAuth
    @ApiOperation(value = "银行卡认证提交")
    @PostMapping(Constant.appPrefex + "/user/bankAuth")
    public ResResult bankAuth(@ApiIgnore CreateAuthVo createAuthVo) {
        return userService.bankAuth(createAuthVo);
    }


    @LoginAuth
    @ApiOperation(value = "绑定银行卡列表")
    @PostMapping(Constant.appPrefex + "/user/bankList")
    public ResResult bankList(@ApiIgnore CreateAuthVo createAuthVo) {
        return userService.bankList(createAuthVo);
    }


    @LoginAuth
    @ApiOperation(value = "上传紧急联系人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "string", required = true),
            @ApiImplicitParam(name = "mobile", value = "手机号码", dataType = "string", required = true),
            @ApiImplicitParam(name = "relation", value = "关系  1：父母  2：子女  3：兄弟姐妹  4：朋友  5：配偶  6：亲戚  7：同学  8：同事  9：其他", dataType = "string", required = true),
            @ApiImplicitParam(name = "name2", value = "姓名2", dataType = "string", required = true),
            @ApiImplicitParam(name = "mobile2", value = "手机号码2", dataType = "string", required = true),
            @ApiImplicitParam(name = "relation2", value = "关系2", dataType = "string", required = true),
            @ApiImplicitParam(name = "addressBooks", value = "通讯录数组", dataType = "string", required = true),
            @ApiImplicitParam(name = "callLogs", value = "通话记录数组", dataType = "string", required = true),
    })
    @PostMapping(Constant.appPrefex + "/user/contactAuth")
    public ResResult contactAuth(@ApiIgnore UserContactByMybatis userContactByMybatis) {
        return userService.contactAuth(userContactByMybatis);
    }



    @ApiOperation(value = "文件上传token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "", dataType = "string", required = true),
    })
    @PostMapping(Constant.appPrefex + "/file/token")
    public ResResult token(@ApiIgnore UFileVo ufileVo) throws Exception {
        return new ResResult(Code.SUCCESS, qiNiuUtil.getUpToken(ufileVo.getKey()));
    }


}
